<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-main>
                <el-form ref="addEntyAttrData" :rules="rules" :model="addEntyAttrData" label-width="115px"
                         label-position="left">
                    <el-form-item label="属性名称 :">
                        <el-select v-model="addEntyAttrData.attrName" filterable placeholder="请选择属性名称" style="width: 100%;" @change="changeAttr">
                            <el-option
                                    v-for="item in attrOptions"
                                    :key="item.name"
                                    :label="item.value"
                                    :value="item.name">
                                {{item.name}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';

    export default {
        name: "addEntyAttr",
        props: {
            enty: {
                type: Object
            }
        },
        data() {
            return {
                attr: this.enty,
                uuid: '',
                addEntyAttrData: {
                    entyUuid: this.enty.uuid,
                    entyName: this.enty.name,
                    attrUuid:'',
                    attrName:''
                },
                entyOptions: [],
                attrOptions:[],
                rules: {
                    entyName: [
                        {required: true, message: '实体名称不能为空', trigger: 'blur'},
                    ],
                    attrName: [
                        {required: true, message: '属性名称不能为空', trigger: 'blur'},
                    ],
                },
                info: '',
                infoShow: false
            }
        },
        watch: {
            enty(val) {
                this.attr = val;
            }
        },
        mounted() {
            // this.queryEnTy();
            this.queryAttr();
        },
        methods: {
            changeAttr(){
                this.attrOptions.forEach( list =>{
                    if(list.name === this.addEntyAttrData.attrName){
                        this.addEntyAttrData.attrName = list.name
                        this.addEntyAttrData.attrUuid = list.uuid
                    }
                })
            },
            //获取属性所有数据
            queryAttr() {
                this.axios.request({
                    method: "get",
                    url: "./smt/kdi/attr/search/findAll",
                    params: {}
                }).then(response => {
                    this.attrOptions = response.data.content;
                }).catch(error => {
                    console.log(error);
                });
            },
            // //查询实体数据
            // queryEnTy() {
            //     this.axios
            //         .request({
            //             method: "get",
            //             url: "./smt/kdi/enty/search/findAll",
            //             params: {}
            //         })
            //         .then(response => {
            //             this.entyOptions = response.data.content;
            //         })
            //         .catch(error => {
            //             console.log(error);
            //         });
            // },
            submit() {
                this.$refs['addEntyAttrData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.addEntyAttrData = replaceSpacesObj(this.addEntyAttrData);
                    this.axios.request({
                        method: "post",
                        url: "./smt/kdi/enatr",
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: {
                            entyUuid: this.attr.uuid,
                            entyName: this.attr.name,
                            attrUuid: this.addEntyAttrData.attrUuid,
                            attrName: this.addEntyAttrData.attrName
                        }
                    }).then(() => {
                        this.endProcessing();
                    }).catch(error => {
                        console.log(error);
                        this.info = '实体属性新增失败';
                        this.infoShow = true;
                    });
                });
            },
            endProcessing() {
                this.info = this.info + '新增成功,两秒后关闭';
                this.infoShow = true;
                setTimeout(() => {
                    this.infoShow = false;
                    this.$emit("editState", false);
                    this.cleanType();
                    this.info = '';
                }, 2000);
            },
            cleanType() {
                this.addEntyAttrData = {
                    entyUuid:'',
                    entyName: '',
                    attrUuid:'',
                    attrName:''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanType();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 360px;
        height: 200px;
        z-index: 100;
    }


</style>